package com.hwl.lab.controller;

import com.hwl.lab.entity.Area;
import com.hwl.lab.entity.Family;
import com.hwl.lab.entity.Peples;
import com.hwl.lab.repository.AreaRepository;
import com.hwl.lab.repository.PeplesRepository;
import com.hwl.lab.service.FamilyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin //允许跨域请求
@RestController
public class FamilyController {

    @Autowired
    private FamilyService familyService;
    @Autowired
    private AreaRepository areaRepository;
    @Autowired
    private PeplesRepository peplesRepository;

    //查询所有（分页）
    @GetMapping("/findFamilyAll/{page}/{size}")
    public Page<Family> findFamilyAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        return familyService.findAll(page,size);
    }
    //查询所有不分页
    @GetMapping("/findFamilyAll")
    public List<Family> findAll(){
        return familyService.findAll();
    }

    @GetMapping("/findByFamilyidF/{familyid}")
    public List<Family> findByUserid(@PathVariable("familyid") String familyid){
        if (familyService.findByFamilyid(familyid) != null) {
            List<Family> familys = new ArrayList<>();
            familys.add(familyService.findByFamilyid(familyid));
            return familys;
        } else {
            return null;
        }
    }
    //添加，修改
    @PostMapping("/saveFamily")
    public void saveFamily(@RequestBody(required=false) Family family){
        familyService.saveFamily(family);
    }

    //通过账号删除
    @PostMapping("/deleteFamily/{id}/{familyid}")
    public void deleteFamily(@PathVariable("id") Long id,@PathVariable("familyid") String familyid){
        peplesRepository.deleteByFamilyid(familyid);
        familyService.deleteById(id);
    }

    //查找所有村
    @GetMapping("/findByLevel")
    public List<Area> findByLevel(){
        return areaRepository.findByLevel("村级");
    }

    //查看家庭成员______________________________________
    @GetMapping("/findByPeple/{familyid}")
    public List<Peples> findByPeple(@PathVariable("familyid") String familyid){
        return peplesRepository.findByFamilyid(familyid);
    }
    //查看家庭成员______________________________________
    @GetMapping("/findByHuzhu/{handname}")
    public List<Peples> findByhandname(@PathVariable("handname") String handname){
        System.out.println(familyService.findByhandname(handname));
        if (familyService.findByhandname(handname) == null) {
            return null;
        } else {
            return findByPeple(familyService.findByhandname(handname).getFamilyid());
        }
    }
    @PostMapping("/savePeples")
    public void savePeple (@RequestBody(required=false) Peples peples) {
        peplesRepository.save(peples);
    }
    @PostMapping("/deletePeple/{chengid}")
    public void deletePeple(@PathVariable("chengid") String chengid) {
        peplesRepository.deleteByChengid(chengid);
    }
}
